package com.auxgroup.hr.front.service.personnal.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCJobCurrVw;
import com.auxgroup.hr.front.dao.repository.PsCJobCurrVwRepository;
import com.auxgroup.hr.front.service.personnal.JobCurrService;

/** 
 * @author wangchangyun
 * @version 创建时间：180523 
 * 人员职称service
 */
@Service("jobCurrService")
public class JobCurrServiceImpl implements JobCurrService {
	private static final Logger logger = LoggerFactory.getLogger(JobCurrServiceImpl.class);
	
	@Autowired
	private PsCJobCurrVwRepository PsCJobCurrVwRepository;
	
	
	/* (non-Javadoc)
	 * @see JobCurrService#getDefaultJobCurr(JobCurrPrimary)
	 */
	@Override
	public PsCJobCurrVw getDefaultJobCurr(JobCurrPrimary jobPrimary) {
		logger.info("getDefaultJobCurr start param:{}",jobPrimary);
		PsCJobCurrVw queryCurr = new PsCJobCurrVw();
		queryCurr.setEmplid(jobPrimary.getEmplid());
		queryCurr.setHrStatus("A");
		queryCurr.setEmplRcd(new BigDecimal(0));
		List<PsCJobCurrVw> jobs = PsCJobCurrVwRepository.findAll(Example.of(queryCurr));
		logger.info("getDefaultJobCurr start success result:{}",jobs);
		if(jobs!=null&&jobs.size()>0){
			return jobs.get(0);
		}
		return null;
	}
	
	

	/* (non-Javadoc)
	 * @see JobCurrService#getJobCurr(java.lang.String)
	 */
	@Override
	public List<PsCJobCurrVw> getJobCurr(JobCurrPrimary jobPrimary) {
		PsCJobCurrVw  queryCurr = new PsCJobCurrVw();
		queryCurr.setHrStatus("A");//只查询在职人员
		queryCurr.setEmplid(jobPrimary.getEmplid());
		if(jobPrimary.getEmplRcd()==null){
			return PsCJobCurrVwRepository.findAll(Example.of(queryCurr));
		}else{
			queryCurr.setEmplRcd(jobPrimary.getEmplRcd());
			return PsCJobCurrVwRepository.findAll(Example.of(queryCurr));
		}
	}

	@Override
	public List<PsCJobCurrVw> getSuperVisor(String emplId, BigDecimal emplRcd,String queryType) {
		PsCJobCurrVw  queryCurr = new PsCJobCurrVw();
		List<PsCJobCurrVw> jobs = new ArrayList<PsCJobCurrVw>();
		queryCurr.setHrStatus("A");//只查询在职人员
		logger.info("getSuperVisor start empLid={},emplRc={},queryType={}",emplId,emplRcd,queryType);
		//直接下属
		if("DIRECT".equals(queryType)){
			queryCurr.setSupervisorId(emplId);
			queryCurr.setcEmplRcd1(emplRcd);
			jobs = PsCJobCurrVwRepository.findAll(Example.of(queryCurr));
		}
		//虚线下属
		if("IN_DIRECT".equals(queryType)){
			queryCurr.setRsReportEmpid(emplId);
			queryCurr.setcEmplRcd2(emplRcd);
			jobs = PsCJobCurrVwRepository.findAll(Example.of(queryCurr));
		}
		return jobs;
	}
	
	@Override
	public Integer getSuperVisorSize(String emplId, BigDecimal emplRcd,String queryType) {
		PsCJobCurrVw  queryCurr = new PsCJobCurrVw();
		Long count = new Long(0);
		queryCurr.setHrStatus("A");//只查询在职人员
		logger.info("getSuperVisor start empLid={},emplRc={},queryType={}",emplId,emplRcd,queryType);
		//直接下属
		if("DIRECT".equals(queryType)){
			queryCurr.setSupervisorId(emplId);
			queryCurr.setcEmplRcd1(emplRcd);
			count = PsCJobCurrVwRepository.count(Example.of(queryCurr));
		}
		//虚线下属
		if("IN_DIRECT".equals(queryType)){
			queryCurr.setRsReportEmpid(emplId);
			queryCurr.setcEmplRcd2(emplRcd);
			count = PsCJobCurrVwRepository.count(Example.of(queryCurr));
		}
		return Integer.valueOf(count.intValue());
	}
	
}
